package com.example.class1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public abstract class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemLongClickListener {
    ListView listView;
    private static final String TAG = "RateListActivity2";
    private ArrayAdapter<Object> listItemAdapter;
    private ArraySet listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);

        //准备数据
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate: " + i);//标题文字
            map.put("ItemDetail", "Detail: " + i);//详细描述
            listItems.add(map);
        }

        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,
                listItems,//listItems数据源
                R.layout.list_item,//listItems的XML布局实现
                new String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.title, R.id.detail});

        //绑定控件
        ListView listView = findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener((AdapterView.OnItemClickListener) this);


        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if (msg.what == 7) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //用于刷新列表
                    // SimpleAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this,
                    //        list,//listItems数据源
                    //       R.layout.list_item,//listItems的XML布局实现
                    //       new String[]{"cname","cval"},
                    //       new int[]{R.id.title, R.id.detail});
                    //listView.setAdapter(listItemAdapter);
                    MyAdapter myAdapter = new MyAdapter(RateListActivity2.this,
                            R.layout.list_item,
                            list);
                    listView.setAdapter(myAdapter);
                }
                super.handleMessage(msg);
            }
        };

        //构造运行对象
        MapTask mapTask = new MapTask();


        Thread t = new Thread((Runnable) mapTask);
        t.start();

    }
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        //获取当前点击的行数据
        HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG, "onItemClick: position=" + position);
        Log.i(TAG, "onItemClick: cname=" + map.get("cname"));
        Log.i(TAG, "onItemClick: cval=" + map.get("cval"));
        Log.i(TAG,"onItemClick: view=" +view) ;
        String titlestr = map.get("title");
        String detailstr = map.get("detail");
                Log.i(TAG,"onItemClickn titleStr="+ titlestr);
                Log.i(TAG,"onItemClickn detai1Str=" + detailstr);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView detail=(TextView) view.findViewById(R.id.detail);
        String title2 =String.valueOf(title.getText());
        String detail2 = String.valueOf( detail.getText());
        Log.i(TAG,"onItemClick: title2=" + title2);
        Log.i(TAG,"onItemClick: detail2=" + detail2);

        Intent rateCalc=new Intent(this,RateListActivity2.class);
        rateCalc.putExtra("title",titlestr);
        rateCalc.putExtra("rate",Float.parseFloat(detailstr));

        startActivity(rateCalc);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        Log.i(TAG, "onItemLongClick: 长按列表项position=" + position);
        //删除操作
        //构造对话框进行确认操作
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: 对话框事件处理");
                listItems.remove(position);
                listItemAdapter.notifyDataSetChanged();
            }
        }).setNegativeButton("否",null);
        builder.create().show();
        Log.i(TAG, "onItemLongClick: size=" + listItems.size());

        return true;
    }

    public class MapTask {
        private static final String TAG = "MapTask";
        Handler handler;


        public void run() {
            //获取网络数据
            Log.i(TAG, "run:Running......");
            //延时
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            ArrayList<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
            try {
                String url = "https://www.boc.cn/sourcedb/whpj/";
                Document doc = Jsoup.connect(url).get();
                String title = doc.title();
                Log.i(TAG, "run:title=" + title);

                Elements trs = doc.getElementsByTag("tr");
                for (Element tr : trs) {
                    Elements tds = tr.getElementsByTag("tr");
                    Log.i(TAG, "run:td.size=" + tds.size());
                    if (tds.size() > 0) {
                        String td1 = tds.get(0).text();
                        String td2 = tds.get(5).text();
                        Log.i(TAG, "run:" + td1 + "==>" + td2);

                        //构造map放入数据
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("cname", td1);
                        map.put("cval", td2);
                        retList.add(map);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            //返回消息给主线程
            //获取msg对象，用于返回主线程

            Message msg = handler.obtainMessage(8, retList);
            msg.obj=retList;
            handler.sendMessage(msg);
            Log.i(TAG, "run: sendMessage ok");
        }
    }
}